/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fyx.secure.web.user;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.fyx.secure.entity.SecureUser;
import org.fyx.secure.ejb.SecureLocal;
import java.io.IOException;
import javax.annotation.PostConstruct;
import org.fyx.mula.ejb.MulaLocal;
import org.fyx.secure.ejb.SecureRoles;
import org.fyx.util.FyxUtility;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author xljan
 */
@ManagedBean
@SessionScoped
public class SecureUserShow {
    private static final Font H1 = FontFactory.getFont("Nimbus Sans L", 18);
    private static final Font H2 = FontFactory.getFont(FontFactory.HELVETICA, 14, Font.BOLD);
    
    @EJB
    private MulaLocal mula;

    @EJB
    private SecureLocal secure;
    private SecureUser user;

    @PostConstruct
    public void postConstruct(){
        FyxUtility.checkRole(SecureRoles.USER_EDIT_MANAGER);
    }

    public SecureUser getUser() {
        return user;
    }

    public StreamedContent getFile() throws DocumentException, FileNotFoundException, IOException {
        Document document = new Document();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, os);
        document.open();
        document.add(new Header("Nadpis", "Obsah nadpisu"));
        document.add(new Paragraph(mula.get("Member report")));
        document.add(new Phrase(mula.get("Name") + ": "));
        document.add(new Phrase(user.getFirstname()));
        
        document.close();
        return new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), 
                "application/pdf",	
                "downloaded_primefaces.pdf");
    }

    public void initUser(int idUser) {
        user = secure.getUserById(idUser);
    }

    public void preProcessPDF(Object document) throws IOException,
            BadElementException, DocumentException {
        Document pdf = new Document();

    }
}
